from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier

#1.获取数据
iris=load_iris()

#2.划分数据集
x_train,x_test,y_train,y_test=train_test_split(iris.data,iris.target,random_state=6)

#3.特征工程：标准化
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)

#4.KNN算法预估器
estimator=KNeighborsClassifier(n_neighbors=3)
estimator.fit(x_train,y_train)
print("\n")
# 5.评估模型
#方法一
y_predict=estimator.predict(x_test)
print("直接比对真实值和预测值：\n",y_test==y_predict)
#方法二
score=estimator.score(x_test,y_test)
print("准确率为：\n",score)
